package com.xiaozheng.JNADemo;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

import java.util.Scanner;

/**
 * What --
 * <br>
 * Describe --
 * <br>
 *
 * @Package: com.xiaozheng.JNADemo
 * @ClassName: CallApi
 * @Author: 小政同学    QQ:xiaozheng666888@qq.com
 * @CreateTime: 2021/2/24 22:49
 */
public class CallApi {
    public static void main(String[] args) {
        // 分配4个字节大小
        Memory memory = new Memory(4);

        Pointer pointer;

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入进程的PID：");
        int pID = scanner.nextInt();
        while (true) {
            // 进程的内存地址
            pointer = ReadMemoryInterface.READ.OpenProcess(2035711, false, pID);
            System.out.println(pointer);
            System.out.println("请输入要读取的内存地址：");
            String address = scanner.next();
            if (pointer != null) {
                boolean flag = ReadMemoryInterface.READ.ReadProcessMemory(pointer, Integer.valueOf(address, 16), memory, 4, null);
                if (!flag) {
                    System.out.println("读取内存失败");
                    return;
                }
                System.out.println("读取内存地址--" + address + "成功：" + memory.getInt(0));

                System.out.println("请输入写入内存的值：");
                memory.setInt(0, scanner.nextInt());
                boolean flag1 = ReadMemoryInterface.READ.WriteProcessMemory(pointer, Integer.valueOf(address, 16), memory, 4, null);
                if (!flag1) {
                    System.out.println("写入内存失败");
                    return;
                }
                System.out.println("写入内存地址--" + address + "成功：" + memory.getInt(0));
            }else {
                System.out.println("读取进程句柄失败！！");
                return;
            }


        }

    }

}